import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from '../components/layout/IndexView.vue'
import { storage } from '../utils/storage'
import { close, start } from '../utils/nprogress'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    meta:{
      title:'后台首页',
      isShow:false,
      missions:true
    },
    component: () => import(/* webpackChunkName: "home" */ '../views/LoginView.vue')
  },
  {
    path: '/',
    name: 'layout',
    component: Layout,
    redirect:'/home',
    meta:{
      title:'后台首页',
      isShow:true,
      missions:true
    },
    children:[
      {
        path: '/home',
        name: 'home',
        meta:{
          title:'后台首页',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "home" */ '../views/HomeView.vue')
      }
    ],
  },
  {
    path: '/AdminPermissions',
    name: 'AdminPermissions',
    component: Layout,
    meta:{
      title:'系统管理',
      isShow:true,
      missions:true
    },
    children:[
      {
        path: '/AccountList',
        name: 'AccountList',
        meta:{
          title:'账号列表',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "AccountList" */ '../views/AdminPermissions/AccountList.vue')
      },
      {
        path: '/AccountType',
        name: 'AccountType',
        meta:{
          title:'账号类型',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "AccountType" */ '../views/AdminPermissions/AccountType.vue')
      },
      {
        path: '/RoleList',
        name: 'RoleList',
        meta:{
          title:'角色列表',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "RoleList" */ '../views/AdminPermissions/RoleList.vue')
      },
      {
        path: '/PermissionsList',
        name: 'PermissionsList',
        meta:{
          title:'权限列表',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "hoPermissionsListme" */ '../views/AdminPermissions/PermissionsList.vue')
      },
      {
        path: '/RolsPermissions',
        name: 'RolsPermissions',
        meta:{
          title:'分配角色权限',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "RolsPermissions" */ '../views/AdminPermissions/RolsPermissions.vue')
      },
      {
        path: '/PersonalContent',
        name: 'PersonalContent',
        meta:{
          title:'个人信息',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "PersonalContent" */ '../views/AdminPermissions/PersonalContent.vue')
      },
      {
        path: '/ModifyPassword',
        name: 'ModifyPassword',
        meta:{
          title:'修改密码',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "ModifyPassword" */ '../views/AdminPermissions/ModifyPassword.vue')
      },
    ],
  },
  {
    path: '/processManagement',
    name: 'processManagement',
    component: Layout,
    meta:{
      title:'审批管理',
      isShow:true,
      missions:true
    },
    children:[
      {
        path: '/BasicSet',
        name: 'BasicSet',
        meta:{ 
          title:'基本设置',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "BasicSet" */ '../views/processManagement/BasicSet.vue')
      },
      {
        path: '/ProcessAccount',
        name: 'ProcessAccount',
        meta:{ 
          title:'基本流程',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "ProcessAccount" */ '../views/processManagement/ProcessAccount.vue')
      },
      {
        path: '/ApprovalAccount',
        name: 'ApprovalAccount',
        meta:{ 
          title:'审批意见',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "ApprovalAccount" */ '../views/processManagement/ApprovalAccount.vue')
      },
    ],
  },
  {
    path: '/ApplicationCenter',
    name: 'ApplicationCenter',
    component: Layout,
    meta:{
      title:'应用中心',
      isShow:true,
      missions:true
    },
    children:[
      {
        path: '/AppCemterView',
        name: 'AppCemterView',
        meta:{ 
          title:'审批意见',
          isShow:true,
          missions:true
        },
        component: () => import(/* webpackChunkName: "AppCemterView" */ '../views/ApplicationCenter/AppCemterView.vue')
      },
    ],
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局前置路由守卫路由守卫
router.beforeEach((to, from, next) => {

  if (to.meta.isAuth) {
    const token: string | null | undefined = storage.get('token')
    if (!token && to.path != '/login') {
      next('/login')
    } else {
      start()
      next()
    }
  } else {
    start()
    next()
  }
})

router.afterEach(() => {
  close()
})
export default router
